using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DynamicDNS.Provider;
using System.Net;

namespace DynamicDNSExample
{
    public partial class ResultForm : Form
    {
        
        public ResultForm()
        {
            InitializeComponent();
        }

        private string username;

        public string UserName
        {
            get { return username; }
            set { username = value; }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        private string host;

        public string Host
        {
            get { return host; }
            set { host = value; }
        }

        private IDynamicDNSProvider provider;

        public IDynamicDNSProvider Provider
        {
            get { return provider; }
            set { provider = value; }
        }

        private void mnuClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void UpdateDNSEntry()
        {
            if (!this.Visible)
            {
                this.Show();
            }
            Cursor.Current = Cursors.WaitCursor;
            txtResults.Text = "Update started...";
            txtResults.Text += "\r\nFound " + (DynamicDNS.DynamicDNSHelper.GetCurrentIPAdresses().Length+1).ToString()+ " IP Adress(es)";
            foreach (IPAddress adress in DynamicDNS.DynamicDNSHelper.GetCurrentIPAdresses())
            {
                // Filter private adresses
                if (adress.ToString().StartsWith("10.") || adress.ToString().StartsWith("169.") || adress.ToString().StartsWith("192.168") || adress.ToString().Equals("127.0.0.1"))
                {
                    txtResults.Text += "\r\nSkipped adress '" + adress.ToString() + "' as it is private.";
                    continue;
                }
                try
                {
                    DynamicDNS.DynamicDNSUpdateResult result = provider.UpdateIP(adress, host, username, password);
                    txtResults.Text += "\r\nUpdate completed for address: " + adress.ToString();
                    switch (result.Status)
                    {
                        case DynamicDNS.DynamicDNSStatus.OK:
                            txtResults.Text += "\r\nThe Update has been successfull!";
                            MessageBox.Show("The Update has been successfull!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                            break;
                        case DynamicDNS.DynamicDNSStatus.Unchanged:
                            txtResults.Text += "\r\nThe Update resulted in no changes.";
                            MessageBox.Show("The Update resulted in no changes.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                            break;
                        case DynamicDNS.DynamicDNSStatus.Error:
                            txtResults.Text += "\r\nThe Update was NOT successfull:\r\n" + result.ErrorMessage;
                            MessageBox.Show("The Update was NOT successfull:\r\n" + result.ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception e)
                {

                    txtResults.Text += "\r\nUpdate FAILED for address: " + adress.ToString();
                    txtResults.Text += "\r\n"+e.Message;
                }
            }
            Cursor.Current = Cursors.Default;
            mnuClose.Enabled = true;
        }
    }
}